package com.qtummatrix.controller;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.qtummatrix.entity.ModelEntity;
import com.qtummatrix.service.ModelService;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("modelAction")
public class ModelController extends ActionSupport implements ModelDriven<ModelEntity> {

    @Autowired
    private ModelService modelService;

    private ModelEntity modelEntity = new ModelEntity();

    public ModelEntity getModelEntity() {
        return modelEntity;
    }

    public void setModelEntity(ModelEntity modelEntity) {
        this.modelEntity = modelEntity;
    }

    public ModelEntity getModel() {
        return modelEntity;
    }

    private Map<String, Object> info = new HashMap<String, Object>();

    public Map<String, Object> getInfo() {
        return info;
    }


    /**
     * 获取所有所属银行、产品系列、产品名称、目标客群和模型状态
     *
     * @return
     */
    public String getmodelindexSelect() {
        //获取所有所属银行
        List<Map<String, Object>> allBankName = modelService.getAllproductBankName();
        //获取所有产品系列
        List<Map<String, Object>> allSerie = modelService.getAllproductSerie();
        //获取所有产品名称
        List<Map<String, Object>> allProductName = modelService.getAllproductName();
        //获取所有目标客群
        List<Map<String, Object>> allCustomType = modelService.getAllcustomType();
        //获取所有产品状态
        List<Map<String, Object>> allState = modelService.getAllmodelState();
        info.put("allBankName", allBankName);//所属银行
        info.put("allSerie", allSerie);//产品系列
        info.put("allProductName", allProductName);//产品名称
        info.put("allCustomType", allCustomType);//目标客群
        info.put("allState", allState);//产品状态
        return SUCCESS;
    }


    /**
     * 获取模型列表
     *
     * @return
     */
    public String getModelList() {
        int bankid = modelEntity.getBankid();
        String modelName = modelEntity.getModelName();
        int seriesid = modelEntity.getSeriesid();
        int product = modelEntity.getProduct();
        int customType = modelEntity.getCustomType();
        int state = modelEntity.getState();
        System.out.println(bankid+","+modelName+","+seriesid+","+product+","+customType+","+state);

        HttpServletRequest request = ServletActionContext.getRequest();
        //当前页码
        int pageNo = Integer.parseInt(request.getParameter("pageNo")) - 1;
        //页面的条数
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));//默认10条数据

        //获取模型列表
        List<Map<String, Object>> modelList = modelService.getModelList(bankid, modelName, seriesid, product,
                customType, state, pageNo, pageSize);
        info.put("list", modelList);

        //获取模型的记录数
        Integer modelSize = modelService.getModelSize(bankid, modelName, seriesid, product, customType, state);
//        System.out.println("产品记录数："+modelSize);

        // 返回的数据列表
        // 当前页数
        info.put("pageNum", pageNo + 1);
        // 总页数
        int pages = modelSize % pageSize == 0 ? modelSize / pageSize : modelSize / pageSize + 1;
        info.put("pages", pages);
        // 总条数
        info.put("total", modelSize);

        modelEntity = new ModelEntity();
        return SUCCESS;
    }

    /**
     * 修改模型状态
     *
     * @return
     */
    public String updateModelState() {
        int id = modelEntity.getId();
        int state = modelEntity.getState();
        System.out.println("修改模型状态,id：" + id + "状态：" + state);

        int count = modelService.updateModelState(id, state);
        System.out.println(count);

        if (count == 1) {
            info.put("updateResult", "1");
        } else {
            info.put("updateResult", "0");
        }
        modelEntity = new ModelEntity();
        return SUCCESS;
    }


    /**
     * 新建模型
     *
     * @return
     */
    public String addModel() {
        System.out.println("开始新建模型");
        String modelName = modelEntity.getModelName();
        int bankid = modelEntity.getBankid();
        String division = modelEntity.getDivision();
        int seriesid = modelEntity.getSeriesid();
        int product = modelEntity.getProduct();
        int customType = modelEntity.getCustomType();
        BigDecimal percentageDown = modelEntity.getPercentageDown();
        int scoreDown = modelEntity.getScoreDown();
        String loanType = modelEntity.getLoanType();
        String area = modelEntity.getArea();

        int count = modelService.addModel(modelName, bankid, division, seriesid, product, customType, percentageDown,
                scoreDown, loanType, area);
        if (count != 0) {
            info.put("addResult", "1");
        } else {
            info.put("addResult", "0");
        }
        modelEntity = new ModelEntity();
        return SUCCESS;
    }


    /**
     * 获取模型详细信息
     *
     * @return
     */
    public String getModelinfo() {
        int id = modelEntity.getId();
//        System.out.println("获取模型详细信息,id："+id);

        List<Map<String, Object>> modelList = modelService.getModelinfo(id);
//        System.out.println(modelList);

        info.put("modelList", modelList);

        modelEntity = new ModelEntity();
        return SUCCESS;
    }


    /**
     * 修改模型详细信息
     *
     * @return
     */
    public String updateModelinfo() {
        int id = modelEntity.getId();
        String modelName = modelEntity.getModelName();
        String division = modelEntity.getDivision();
        BigDecimal percentageDown = modelEntity.getPercentageDown();
        int scoreDown = modelEntity.getScoreDown();
        String loanType = modelEntity.getLoanType();
        String area = modelEntity.getArea();
//        System.out.println("修改模型详细信息,id："+id+","+modelName+","+division+","+percentageDown+","+scoreDown+",
//        "+loanType+","+area);

        int count = modelService.updateModelinfo(id, modelName, division, percentageDown, scoreDown, loanType, area);
//        System.out.println(count);

        if (count == 1) {
            info.put("updateResult", "1");
        } else {
            info.put("updateResult", "0");
        }

        modelEntity = new ModelEntity();
        return SUCCESS;
    }


    /**
     * 获取模型对应的模组的问题列表
     *
     * @return
     */
    public String getQuestionList() {
        int id = modelEntity.getId();
        System.out.println("模型id：" + id);

        HttpServletRequest request = ServletActionContext.getRequest();
        //当前页码
        int pageNo = Integer.parseInt(request.getParameter("pageNo")) - 1;
        //页面的条数
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));//默认10条数据

        //获取问题列表
        List<Map<String, Object>> questionList = modelService.getQuestionList(id, pageNo, pageSize);
        System.out.println(questionList);
        info.put("list", questionList);

        //获取问题的记录数
        Integer questionSize = modelService.getQuestionSize(id);
        System.out.println("产品记录数：" + questionSize);

        // 返回的数据列表
        // 当前页数
        info.put("pageNum", pageNo + 1);
        // 总页数
        int pages = questionSize % pageSize == 0 ? questionSize / pageSize : questionSize / pageSize + 1;
        info.put("pages", pages);
        // 总条数
        info.put("total", questionSize);

        modelEntity = new ModelEntity();
        return SUCCESS;
    }
}
